package com.showcai.service.impl;

import com.showcai.dao.OrderItemMapper;
import com.showcai.module.*;
import com.showcai.module.activity.ActivityExecResoult;
import com.showcai.module.activity.ActivityParameters;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import com.showcai.util.GenerateID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-22.
 */
@Service
public class OrderItemServiceImpl extends ServiceBase implements OrderItemService {

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    ActivityService activityService;

    @Override
    public Boolean modifyOrderItem(OrderItem orderItem) {
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
        return true;
    }

    @Override
    public OrderItem getOrderItemByOrderItemId(Long orderItemId) {
        return orderItemMapper.getOrderItemByOrderItemId(orderItemId);
    }

    @Override
    public List<OrderItem> selectOrderItemList(Map<String, String> map) {
        return orderItemMapper.selectOrderItemList(map);
    }

    @Override
    public int updateOrderItemStatus(Map<String, String> map) {
        return orderItemMapper.updateOrderItemStatus(map);
    }

    @Override
    public Integer createOrderItem(Long orderId, List<Long> productEntityIdList, Long userId, List<Long> activittyIdList) {

        List<ProductEntity> productEntityList = new ArrayList<>();
        User user = userService.getUserById(userId);
        List<Activity> activityList=new ArrayList<>();
        if(activittyIdList!=null && activittyIdList.size()>0){
            activityList = activityService.getActivityListByIds(activittyIdList);
        }
        WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
        //TODO 暫時沒有活動，以後再説
        Integer price = 0;
        for (Long productEntityId : productEntityIdList
                ) {

            for (Activity activity:activityList
                 ) {
                //activity.getExecClass().
                if(activity.getActivityType()==Activity.ACTIVITY_TYPE_REDUCE_PRODUCT_ENTITY){
                    ActivityExecService  activityExecService = (ActivityExecService) wac.getBean(activity.getExecClass());
                    ActivityParameters activityParameters=new ActivityParameters();
                    activityParameters.setProductEntityId(productEntityId);
                    ActivityExecResoult activityExecResoult= activityExecService.exec(activityParameters);
                    productEntityId=activityExecResoult.getProductEntityId();
                }

            }
            if(productEntityId==null){
                continue;
            }
                ProductEntity productEntity = productEntityService.getProductEntityById(productEntityId);
                price += productEntity.getPrice();
                productEntityList.add(productEntity);

                OrderItem orderItem = new OrderItem();
                orderItem.setOrderId(orderId);
                orderItem.setOrderItemId(GenerateID.CreatetID());
                orderItem.setProductEntityId(productEntityId);
                orderItem.setCreateTime(new Date());
                orderItem.setStatus(OrderService.UNPAY);
                orderItem.setPrice(productEntity.getPrice());
                orderItemMapper.insertOrderItem(orderItem);


        }
        return price;
    }

    @Transactional
    @Override
    public Boolean cancelOrderItem(Long orderItemId) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        Long orderId = orderItem.getOrderId();
        orderItem.setStatus(OrderService.CANCEL);
        if (orderItemMapper.updateOrderItemById(orderItem) != 1) {
            throw new MyRunTimeExcption("取消失败");
        }

        Order order = orderService.getOrderById(orderId);
        orderService.modifyOrder(order);
        return true;
    }

    @Override
    public Boolean modifyOrderItemStatus(Long orderItemId, Byte status) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        orderItem.setStatus(status);
        orderItemMapper.updateOrderItemById(orderItem);

        return true;
    }

    @Override
    public Boolean completeOrderItem(Long orderItemId) {
        logger.info("completeOrderItem");
        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);

        if (orderItem == null) {
            throw new MyRunTimeExcption("订单项目没有找到！");
        }
        orderItem.setStatus(OrderService.COMPELTEED);


        orderItemMapper.updateOrderItemById(orderItem);

        checkOrderItem(orderItem.getOrderId());
        return true;
    }

    private void checkOrderItem(Long orderId) {
        Order order = orderService.getOrderById(orderId);
        List<OrderItem> orderItemList = getOrderItemByOrderId(orderId);
        for (OrderItem orderItems : orderItemList) {
            if (orderItems.getStatus() != OrderService.COMPELTEED && orderItems.getStatus() != OrderService.REFUNDED) {
                return;
            }
        }
        order.setStatus(OrderService.CLOSE);
        orderService.modifyOrder(order);
    }

    @Override
    public List<OrderItem> getOrderItemByOrderId(Long orderId) {
        return orderItemMapper.selectOrderItemListByOrderId(orderId);
    }
}
